#ifndef CMD_TELNETED_H
#define CMD_TELNETED_H

#include "mina/tcp_acceptor_config.h"
#include "mina/tcp_acceptor.h"
#include "mina/tcp_message_context.h"
#include "mina/service_url.h"

#include "cmd_telnet_reader.h"

#include <map>

namespace vpm 
{
	namespace cmo 
	{
		using namespace vpm::mina;

		class CmdTelneted : public IoHandler 
		{
		public:

			CmdTelneted()
			{
				ACE::init();
				m_tcpConfig.get_session_config().set_tcp_mode(TcpSessionConfig::TCP_SYNCH);
				m_tcpServer.reset(new TcpAcceptor<>(m_tcpConfig,*this));
				m_tcpServer->bind(ServiceUrl("tcp://0.0.0.0:8022"));		
			}

			virtual void session_opened(IoSession & session)
			{
				CmdTelnetReaderPtr telnetReader(new CmdTelnetReader(session));
				m_telnetReaders.insert(std::make_pair(&session, telnetReader));

				TcpMessageContext msg((char *)WONT_ECHO,sizeof(WONT_ECHO)-1);
				session.write(msg);
				TcpMessageContext msg1((char*)DO_XDISPLOC,sizeof(DO_XDISPLOC) -1);
				session.write(msg1);
			}

			virtual void session_closed(IoSession & session)
			{
			 	m_telnetReaders.erase(&session);
			}
   
			virtual void message_received(IoSession & session,MessageContext & msg)
			{

			}

		private:
			std::auto_ptr<IoAcceptor > m_tcpServer; 
			TcpAcceptorConfig         m_tcpConfig; 
			//std::set<CmdTelnetReaderPtr> m_telnetReaders;
			std::map<IoSession * ,CmdTelnetReaderPtr> m_telnetReaders;
		};
		
	}
}

#endif // 
 
